<?php
/**
 * 客服后台操作页面
 * @author wjb
 * @date 2016/11/20
 */
require_once __DIR__ . '/__init.php';
use Lib\Db;
function get_ip_by_url($url){
    //获取它的主机部分
    $url = parse_url($url);
    //gethostbyname 获取对应IP
    $url['host'] = isset($url['host']) ? $url['host'] : '';
    $ip = gethostbyname($url['host']);

    if($ip){
        return $ip;
    }else{
        return false;
    }
}
$before_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$before_ip = get_ip_by_url($before_url);
$now_url ='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$now_ip = get_ip_by_url($now_url);
//允许的IP
$ip_arr = array(
    '103.199.102.123',
    '103.199.102.121',
);
//if(in_array($before_ip,$ip_arr)){
//    if(!in_array($now_ip,$ip_arr)){
//        exit('Please get out!');
//    }
//}else{
//    exit('Please get out!');
//}


//if(strcmp($before_ip,$now_ip)!=0){
//    exit('Please get out!');
//}

_session_start();
//数据库
$db = Db::instance('laychat');
//服务器url
$local = config_url(2);
//获取用户uid
$cook = $_GET['uid'];
//在后台admins表中查
$res_admin = $db->query("select * from `admins` where id ={$cook}");

//更新在线状态
$db->update('admins')->col('online', 'online')->where('id=:id')->bindValue('id', $cook)->query();
$db->update('admins')->col('chat', 1)->where('id=:id')->bindValue('id', $cook)->query();
//判断是否有昵称
if(isset($res_admin[0]['name'])){
    $name = $res_admin[0]['name'];
}else{
    $name = $res_admin[0]['username'];
}
////后台查到用户后，再前台users表中找kf-****的用户
//$res = $db->query("select * from `users` where username ='{$custom_name}'");
$_SESSION['laychat'] = array(
    'id'       => "admin-".$cook,
    'username' => $name,
    'sign'     => '客服',
    'avatar'   => config_img(2),
);

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>pangu客服系统</title>
    <script type="text/javascript">
        var pg_local = '<?php echo SERVICE_URL;?>';
        var ws_ip = '<?php echo WORKMAN_IP;?>';
    </script>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/base.css?v1">
    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/js/json2.js"></script>
    <script src="/static/js/swfobject.js"></script>
    <script src="/static/js/web_socket.js"></script>
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/laychat.js"></script>

    <script type="text/javascript">
        localStorage.phpIp = returnCitySN["cip"];
    </script>
</head>
<body>

<div class="main-box">
    <div class="desc">
        <script type="text/javascript">
            laychat.open();
        </script>

        <!--        <img src="--><?php //echo $_SESSION['laychat']['avatar'];?><!--" style="width:40px">-->
        <!--        --><?php //echo $_SESSION['laychat']['username'];?>
        <?php
            $now_admin = $db->query("select `online` from `admins` where id ={$cook}");
            if($now_admin[0]['online']=='offline'){
                echo "<b style='color:#FF9933;font-size:18px;'>您目前已经离线</b>";
            }
        ?>
        <p id="offline"></p>
    </div>

    <div class="chat-tip">
        <p class="chat-tipimg"><i class="layui-icon" style="font-size: 100px; color: #666;">&#xe611;</i>  </p>
        <p>点击右侧历史会话可以打开聊天窗口</p>
        <p>如要离开，请关闭该页面</p>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        var screenW=$(document).width(),
            screenH=$(document).height(),
            offsetT=(screenH-500)/2 + "px",
            offsetL=(screenW-600-262)/2+"px";
        $(".chat-tip").css({"position":"absolute","top":offsetT,"left":offsetL});
    })
</script>


</body>
</html>
